﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Text;
using BusinessObjects;
using BusinessObjects.Constants;
using DataEntities;
using DataProvider;
using SQLDataProvider;
using System.Linq;

namespace BusinessServices
{
    public class WeiZhangService
    {
        private readonly IBaseRepository<WeiZhangInfo> _weiZhangRepository;

        public WeiZhangService()
        {
            _weiZhangRepository = new BaseRepository<WeiZhangInfo>();
        }

        /// <summary>
        /// 根据客户编号获取简要违章信息
        /// </summary>
        /// <param name="keHuId">客户编号</param>
        public SimpleWeiZhangInfoContract GetSimpleWeiZhangInfoByKeHuId(int keHuId)
        {
            return GetSimpleWeiZhangInfo(o => o.iKeHuId == keHuId);
        }

        /// <summary>
        /// 根据汽车编号获取简要违章信息
        /// </summary>
        /// <param name="carId">汽车编号</param>
        public SimpleWeiZhangInfoContract GetSimpleWeiZhangInfoByCarId(int carId)
        {
            return GetSimpleWeiZhangInfo(o => o.iCarid == carId);
        }

        /// <summary>
        /// 根据客户编号获取详细违章信息
        /// </summary>
        /// <param name="keHuId">客户编号</param>
        public List<WeiZhangInfo> GetWeiZhangInfoByKeHuId(int keHuId)
        {
            return _weiZhangRepository.GetAll(o => o.iKeHuId == keHuId, JoinKeHuAndCarAndChuZu);
        }

        /// <summary>
        /// 根据出租单编号获取违章信息
        /// </summary>
        /// <param name="chuZuDanId">出租单编号</param>
        public List<WeiZhangInfo> GetWeiZhangInfoByChuZuDanId(int chuZuDanId)
        {
            return _weiZhangRepository.GetAll(o => o.iCZDId == chuZuDanId);
        }

        //Join查询
        private DataLoadOptions JoinKeHuAndCarAndChuZu()
        {
            var loadOption = new DataLoadOptions();
            loadOption.LoadWith<WeiZhangInfo>(o => o.KeHuInfo);
            loadOption.LoadWith<WeiZhangInfo>(o => o.CarInfo);
            return loadOption;
        }

        #region 获取并构建违章信息
        private SimpleWeiZhangInfoContract GetSimpleWeiZhangInfo(Func<WeiZhangInfo, bool> condition)
        {
            var wzInfo = _weiZhangRepository.GetAll(condition, JoinCarInfo);
            var result = new SimpleWeiZhangInfoContract();
            var strBuilder = new StringBuilder();

            wzInfo.ForEach(wz => GenerateStr(wz, ref strBuilder));
            result.Count = wzInfo.Count;
            result.Unsettled = wzInfo.Count(o => !o.bWZZF);
            result.Content = strBuilder.Length == 0 ? Constants.NoWeiZhangInfo : strBuilder.ToString();
            return result;
        }

        private DataLoadOptions JoinCarInfo()
        {
            var loadOption = new DataLoadOptions();
            loadOption.LoadWith<WeiZhangInfo>(o => o.CarInfo);
            return loadOption;
        }

        private void GenerateStr(WeiZhangInfo wz, ref StringBuilder strBuilder)
        {
            strBuilder.AppendFormat(Constants.SimpleWeiZhangInfo, wz.CarInfo.sCType, wz.sWZAddr, wz.sWZDetail, wz.dWZJE, wz.tWZDate, wz.bWZZF ? "已处理" : "未处理");
        }
        #endregion

        /// <summary>
        /// 添加违章信息
        /// </summary>
        public WeiZhangInfo Add(WeiZhangInfo weiZhangInfo)
        {
            return _weiZhangRepository.Add(weiZhangInfo);
        }

        /// <summary>
        /// 更新指定出租单的所有违章信息
        /// </summary>
        /// <param name="czdId">出租单编号</param>
        /// <param name="update">包含更新操作的委托，在此委托中进行更新操作</param>
        /// <returns>返回更新后的数据集</returns>
        public List<WeiZhangInfo> Update(int czdId, Action<WeiZhangInfo> update)
        {
            return _weiZhangRepository.UpdateAll(w => w.iCZDId == czdId, update);
        }
    }
}
